package nckd.wnqjt.tmc.tda.plugin.form;

import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.entity.datamodel.events.PackageDataEvent;
import kd.bos.entity.report.ReportColumn;
import kd.bos.entity.report.ReportQueryParam;
import kd.bos.form.field.BasedataEdit;
import kd.bos.form.field.events.BeforeF7SelectEvent;
import kd.bos.form.field.events.BeforeF7SelectListener;
import kd.bos.orm.query.QCP;
import kd.bos.orm.query.QFilter;
import kd.bos.report.plugin.AbstractReportFormPlugin;
import kd.bos.servicehelper.BusinessDataServiceHelper;
import kd.bos.servicehelper.user.UserServiceHelper;

import java.util.Arrays;
import java.util.EventObject;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author zhujintao
 * @date 2024/11/05
 * @desc 金融机构授信
 */
public class FinanceOrgCreditFormPlugin extends AbstractReportFormPlugin implements BeforeF7SelectListener {
    @Override
    public void initDefaultQueryParam(ReportQueryParam queryParam) {
        super.initDefaultQueryParam(queryParam);
    }

    @Override
    public void registerListener(EventObject e) {
        super.registerListener(e);
        this.addF7Listener(this, "nckd_org");
    }

    private void addF7Listener(BeforeF7SelectListener form, String... f7Names) {
        BasedataEdit f7 = null;
        String[] var4 = f7Names;
        int var5 = f7Names.length;

        for (int var6 = 0; var6 < var5; ++var6) {
            String f7Name = var4[var6];
            f7 = this.getControl(f7Name);
            if (f7 != null) {
                f7.addBeforeF7SelectListener(form);
            }
        }

    }

    @Override
    public void beforeQuery(ReportQueryParam queryParam) {
        super.beforeQuery(queryParam);
    }

    @Override
    public void beforeF7Select(BeforeF7SelectEvent beforeF7SelectEvent) {
        QFilter qFilter = new QFilter("user", QCP.equals, UserServiceHelper.getCurrentUserId())
                .and("role.number", QCP.equals, "qfx02");
        // 用户和通用角色关系
        DynamicObject[] dynamicObjects = BusinessDataServiceHelper.load("perm_userrole", "org", qFilter.toArray());
        List<Long> orgIdList = Arrays.stream(dynamicObjects).map(t -> t.getDynamicObject("org").getLong("id")).collect(Collectors.toList());

//        // 根据用户查询用户组
//        QFilter qFilter = new QFilter("user", QCP.equals, UserServiceHelper.getCurrentUserId());
//        // 用户组职员关系
//        DynamicObject[] bosUsergroupstaffs = BusinessDataServiceHelper.load("bos_usergroupstaff", "usergroup", qFilter.toArray());
//        List<Long> usergroup = Arrays.stream(bosUsergroupstaffs).map(t -> t.getDynamicObject("usergroup").getLong("id")).collect(Collectors.toList());
//
//        // 根据用户组查询组织(权限控制对象)
//        QFilter qFilter2 = new QFilter("usrgrpid", QCP.in, usergroup)
//                .and("role.number", QCP.equals, "qfx02");
//        // 用户组角色关系-多类别
//        DynamicObject[] permUsrgrproles = BusinessDataServiceHelper.load("perm_usrgrprole", "dimid", qFilter2.toArray());
//        List<Long> orgIdList = Arrays.stream(permUsrgrproles).map(t -> t.getDynamicObject("dimid").getLong("id")).collect(Collectors.toList());

        // 权限过滤
        beforeF7SelectEvent.addCustomQFilter(new QFilter("id", QCP.in, orgIdList));
    }

    private static final String MERGECOLUM = "bankbankcateid";//合并单元格字段
    private static final String CONDITION_FIELD = "bankbankcateid";//条件字段

    @Override
    public void setMergeColums(List<String> columns) {
        columns.add(MERGECOLUM);
        super.setMergeColums(columns);
    }

    @Override
    public void packageData(PackageDataEvent packageDataEvent) {
        ReportColumn c = (ReportColumn) packageDataEvent.getSource();
        //当前读取的列
        String fieldKey = c.getFieldKey();
        if (fieldKey.equals(CONDITION_FIELD)) {
            //某列某行单元格的数据
            String s = packageDataEvent.getFormatValue().toString();
            //获取上一行数据
            String condition_prevalue = this.getPageCache().get("srstatusa_prevalue");
            if (condition_prevalue == null || condition_prevalue.isEmpty()) {
                ;
            } else if (!condition_prevalue.equals(s)) {
                //设置取消某列当前行与上一行的合并
                packageDataEvent.getNoMergeKey().add(MERGECOLUM);
            }
            //把当前物料所在单元格数据保存到页面缓存中
            this.getPageCache().put("srstatusa_prevalue", s);
        }
        super.packageData(packageDataEvent);
    }

}